﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.library.Helper;
using george.Data;
using NHibernateVertica;
using VerticaAPI.model;

namespace VerticaAPI
{
    /// <summary>
    /// 补货需求单
    /// </summary>
    [Description("补货需求单")]
    public class DwSupplyRoundHandler:BaseHandler
    {
        public VerticaDataHelper Helper { get; set; } = new VerticaDataHelper();

        [MustLogin]
        [Description("补货需求单")]
        [ReturnType(typeof(PaperData<DwSuppleRoundModel>))]
        public ApiResult GetDwSupplyRound([ParamDescription("关键字")] string Keys = "",
            [ParamDescription("页码")] int Page = 1,
            [ParamDescription("页数")] int Size = 10)
        {
            var strSql = @"select a.*,b.ShopName,s.SizeName from dw.dw_supply_round a
                left join dw.dw_Shop b on a.StockID = b.StockID
                left join ods.ods_erp_SD_Mat_Size s on a.SizeID = s.SizeID ";
            if (!string.IsNullOrWhiteSpace(Keys))
                strSql += $" where b.ShopName like '%{Keys}%'";
            var cmd = Helper.GetSqlStringCommand(strSql);
            var dt = Helper.ExecuteDataTable(cmd);
            var list = ModelConvertHelper<DwSuppleRoundModel>.ConvertToModel(dt);
            var result = PaperData<DwSuppleRoundModel>.Create(list.Count, Size, Page);
            result.Items = list.Skip(result.Skip).Take(result.Limt).ToList();
            return Success(result);
        }

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="Keys"></param>
        /// <returns></returns>
        public DataTable ReturnDwSuppleRound(string Keys = "")
        {
            var strSql = @"select a.*,b.ShopName,s.SizeName from dw.dw_supply_round a
                left join dw.dw_Shop b on a.StockID = b.StockID
                left join ods.ods_erp_SD_Mat_Size s on a.SizeID = s.SizeID where 1=1 ";
            if (!string.IsNullOrWhiteSpace(Keys))
                strSql += $" {Keys}";
            var cmd = Helper.GetSqlStringCommand(strSql);
            return Helper.ExecuteDataTable(cmd);
        }

        /*
        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <param name="Keys">一个拼接出来的查询条件</param>
        /// <returns>字符串类型的sql查询条件</returns>
        public string GetSearchCondition(string Keys)
        {
            string strWhere = string.Empty;
            if (!string.IsNullOrWhiteSpace(Keys))
            {
                var field = Keys.Split(',');
                foreach (var v in field)
                {
                    var keyValuePair = v.Split(':');
                    var key = keyValuePair[0]?.Trim();
                    strWhere += key + " like '%" + keyValuePair[1] + "%'";
                    //判断如果有N个参数，要在参数之后增加N个AND
                    strWhere += " AND ";
                }
                //删除最后一个AND
                strWhere = strWhere.Substring(0, strWhere.Length - 4);
            }

            return Keys;
        }
        */
    }
}
